package com.spring_mvc.web.listener;

import com.spring_mvc.config.Spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();

//        读取web.xml中的全局参数
//        String contextConfig = context.getInitParameter("contextConfig");
//        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfig);


        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Spring.class);
        context.setAttribute("app", app);
        System.out.println("spring创建成功");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
